package kata;

import java.util.ArrayList;
import java.util.Collections;

public class StraightFlushDecider extends WinCaseDecider {

	@Override
	protected String winnerFound(ArrayList<Card> handOne,
			ArrayList<Card> handTwo) {
		String winningPlayer = "";
				
		Collections.sort(handOne);
		Collections.sort(handTwo);
		
		boolean checkForStraightFlushOne = checkForFlush(handOne) && checkForStraight(handOne);
		boolean checkForStraightFlushTwo = checkForFlush(handTwo) && checkForStraight(handTwo);

		if (checkForStraightFlushOne && checkForStraightFlushTwo){
			return winnerDecidedByHighCard(handOne, handTwo);
		}
		if (checkForStraightFlushOne)
			winningPlayer = "1";
		if (checkForStraightFlushTwo)
			winningPlayer = "2";
		
		return "Player " + winningPlayer + " wins with a straight flush.";
	}
	
	private String winnerDecidedByHighCard(ArrayList<Card> handOne,
			ArrayList<Card> handTwo) {
		if(handOne.get(5).getValue()>handTwo.get(5).getValue()){
			return "Player One wins with high card " + handOne.get(5).getValue() + handOne.get(5).getSuit().getSuitChar();
		}
		if(handTwo.get(5).getValue()>handTwo.get(5).getValue()){
			
		}
		return null;
	}

	private boolean checkForStraight(ArrayList<Card> hand) {
		boolean straightCheck = true;
		int previousCard = hand.get(0).getValue() - 1;
		for (Card card : hand) {
			if (card.getValue() == previousCard + 1) {
				previousCard = card.getValue();
			} else {
				straightCheck = false;
			}
		}
		return straightCheck;
	}

	private boolean checkForFlush(ArrayList<Card> hand) {
		int handSuitCount = 0;
		boolean handFlush = false;
		Suit handSuit = hand.get(0).getSuit();

		for (Card card : hand) {
			if (card.getSuit() == handSuit) {
				handSuitCount++;
			}
		}
		if (handSuitCount == 5) {
			handFlush = true;
		}
		return handFlush;
	}

}
